import { Injectable } from '@angular/core';
import { defaultHttpOptionsFromConfig, RequestConfig } from './http-utils';
import { map, Observable, tap } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { PageLink } from '@shared/models/page/page-link';
import { PageData } from '@shared/models/page/page-data';
import { Video, VideoInfo } from '@app/shared/models/video.models';
import { CustomerConfigService } from './customer-config.service';
import { EntityType } from '@app/shared/public-api';
import { VideoId } from '@app/shared/models/id/video-id';

@Injectable({
  providedIn: 'root'
})
export class VideoService {

  constructor(
    private http: HttpClient,
    private  customerconfigService:CustomerConfigService,
  ) { }

  public getVideoInfos(customerId: string, pageLink: PageLink, type: string = '',
                               config?: RequestConfig): Observable<PageData<VideoInfo>> {
    return this.http.get<PageData<VideoInfo>>(`/api/video/list${pageLink.toQuery()}&type=${type}`,
      defaultHttpOptionsFromConfig(config)).pipe(

        tap(num => console.log(num)),
map(i => {
 i.data= i.data.map(o=>{

    return {...o,id:{id:(o.id as unknown as string),entityType:EntityType.VIDEO}};

  })

  console.log(i)
  return i;
})

      );
  }

  public getVideo(videoId: any, config?: RequestConfig): Observable<Video> {
    return this.http.get<Video>(`/api/asset/${videoId.id}`, defaultHttpOptionsFromConfig(config));
  }

  public getVideoInfo(videoId: any, config?: RequestConfig): Observable<VideoInfo> {
    return this.http.get<VideoInfo>(`/api/video/${videoId.id}`, defaultHttpOptionsFromConfig(config));
  }

  public saveVideo(v: any, config?: RequestConfig): Observable<Video> {
    v.orgCode=this.customerconfigService.config.orgConfig.orgCode;
    v.orgName=this.customerconfigService.config.orgConfig.orgName;
    return this.http.post<Video>('/api/video', v, defaultHttpOptionsFromConfig(config));
  }

  public deleteVideo(videoId: any, config?: RequestConfig) {
    return this.http.delete(`/api/video/${videoId.id}`, defaultHttpOptionsFromConfig(config));
  }


}
